﻿using SPWebShell.entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Web.Services.Protocols;

namespace SPWebShell.cmdlet
{
    /// <summary>
    /// Gets all site collection users in the specified SharePoint site collection
    /// </summary>
    [Cmdlet(VerbsData.Update, "SPSiteUser")]
    public class GetSPSiteUser : CmdLetBase
    {

        public GetSPSiteUser()
        {

        }
        
        [Parameter(Mandatory = true, HelpMessage="The absolute URL to the SharePoint site collection. e.g. http://server/sites/SiteA/")]
        [ValidateNotNullOrEmpty]
        public string Url { get; set; }

        [Parameter(Mandatory = true, HelpMessage = "The login of the user. e.g. DOMAIN\\user for SP 2007 and SP 2010. For SP 2013 this would be different")]
        [Alias("user","loginname")]
        public string Login { get; set; }

        [Parameter(Mandatory = true, HelpMessage = "The new display name of the user")]
        [Alias("name")]
        public string Display { get; set; }

        [Parameter(Mandatory = true, HelpMessage = "The new email address of the user")]
        public string Email { get; set; }

        [Parameter(Mandatory = false, HelpMessage = "Any additional notes for the user")]
        public string Notes { get; set; }

        protected override void ProcessRecord()
        {
            try
            {
                VerboseLogger("Inside Cmdlet UpdateSPSiteUser");
                VerboseLogger("Start time: " + DateTime.Now.ToString());
                ISPRepository repos = base.SPRepos;
                repos.UpdateUserInformation(this.Url, this.Login, this.Display, this.Email,this.Notes);
                VerboseLogger("End time: " + DateTime.Now.ToString());
                base.ProcessRecord();
            }
            catch (SoapException ex)
            {
                string message = ex.Detail.OuterXml;
                throw new Exception(message, ex);
            }
            finally
            {
                VerboseLogger("Exiting Cmdlet SPSiteUsers");
            }
        }
    }
}
